home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: Generating pointer to struct from string
- Date: 27 Feb 96 22:55:01 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.825461701@rscernix>
- References: <4grk4g$hep@fmsu03.fm.intel.com>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4grk4g$hep@fmsu03.fm.intel.com> vdalvi@mcd.intel.com (Vishram Dalvi) writes:
-
- >Say I have 3 structs aaa, bbb and ccc of type foo:
- >
- >struct foo {
- > int flag;
- > char name[30]
- >} aaa, bbb, ccc;
- >
- >I assign some values to the data members of all 3 structs. Now I prompt the
- >user for the name of one of these structs. I read in the user entry into a
- >char array. Assume that the user entered "aaa". How can I generate a pointer
- >to the struct "aaa" from the user entry?
- >I do not want a switch/case table (if user entered "aaa", access members of
- >"aaa", if user entered "bbb"...).
- >How do I cast a string into a struct address??
-
- You don't and you can't use a switch, either. Your options are:
-
- 1. An ugly if/else if/else sequence, testing for all the possible
- variable names.
-
- 2. A symbol table. K&R2 will show you how to implement a symbol table
- using a binary tree.
-
- For a trivial case like yours, when everything is known at compile time
- and the number of symbols is very small, the first approach is the way
- to go. That is, unless you can redesign your application, using an array
- of struct's.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-